package org.mathig.poker.player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.mathig.poker.Action;
import org.mathig.poker.AfterBetActions;
import org.mathig.poker.AfterNothingActions;
import org.mathig.poker.Card;
import org.mathig.poker.Context;
import org.mathig.poker.Player;
import org.mathig.poker.PlayerWrapper;
import org.mathig.poker.table.Table;

public class RandomPlayer implements Player {

	private static int count = 0;

	private int number;

	public RandomPlayer() {
		number = count++;
	}

	public boolean doYouMuck(Context table) {
		return true;
	}

	private Random random = new Random();

	private Card[] cards;

	public String getName() {
		return "player" + number;
	}

	public void receive(Context context, Card[] cards) {
		this.cards = cards;
		System.out.println(getName() + " > cards = " + Arrays.toString(this.cards));
	}

	private void printContext(Context context) {
		// System.out.println(getName() + " > cards = " + Arrays.toString(cards) + ", board = " + context.getBoard());
		// System.out.println(getName() + " > pot = " + context.getPot() + ", stack = " + context.getStack(this));
		// System.out.println(getName() + " > highest bet = " + context.getBet() + ", actual bet = " + context.getBet(this));
	}

	public Action response(Context context, AfterNothingActions actions) {
		printContext(context);
		switch (random.nextInt(10)) {
		case 0:
			if (context.getStack(this) > 10 * context.getBigBlind())
				return actions.bet(Math.min(context.getStack(this), 3 * context.getBigBlind()));
			else
				return actions.allIn();
		case 1:
		case 2:
		case 3:
		case 4:
			if (context.getStack(this) > 6 * context.getBigBlind())
				return actions.check();
			else
				return actions.allIn();
		default:
			return actions.fold();
		}
	}

	public Action response(Context context, AfterBetActions actions) {
		printContext(context);
		switch (random.nextInt(10)) {
		case 0:
			if (context.getStack(this) > 10 * context.getBigBlind())
				return actions.raise(Math.min(context.getStack(this), 3 * context.getBet()));
			else
				return actions.allIn();
		case 1:
		case 2:
		case 3:
		case 4:
			if (context.getStack(this) > 6 * context.getBigBlind())
				return actions.call();
			else
				return actions.allIn();
		default:
			return actions.fold();
		}
	}

	public static void main(String[] args) {
		List<PlayerWrapper> players = new ArrayList<PlayerWrapper>();
		for (int i = 0; i < 4; i++)
			players.add(new PlayerWrapper(new RandomPlayer()));
		Table table = new Table(players.size());
		table.seat(players);
		table.play();
	}

}
